<template>
  <div class="overview">
    <ul class="static">
      <li v-for="(item, index) in countData" :key="index">
        <p class="static-title">{{ item.title }}</p>
        <div class="counts">
          <span class="count">{{ item.count }}</span>
          <img src="@/assets/images/quxiaojine.png" />
        </div>
      </li>
    </ul>

    <div class="message">
      <p class="title">消息通知</p>
      <ul>
        <li v-for="item in overvieData.list" :key="item.messageId">
          <div class="message_info">
            <span :class="['tag', { goods: item.messageType === 'AUDIT_PASS' }, { account: item.messageType === 'QUALIFICATION_SOON_EXPIRE' }]">{{ consts.MessageType[item.messageType] }}</span>
            <span class="info">{{ item.supplierMessage }}</span>
            <Link label="查看详情" class="oper" @click="() => onDetailClick(item.messageType)" :underline="true" />
          </div>
        </li>
      </ul>
    </div>
  </div>
</template>

<script setup>
import { reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import api from '@/api'
import consts from '@/consts'
import Link from '@/components/button/link.vue'

const router = useRouter()

const overvieData = reactive({
  list: []
})

const countData = [
  {
    title: '待付款订单',
    name: 'unPaidCount',
    count: '-'
  },
  {
    title: '待发货订单',
    name: 'unDeliverCount',
    count: '-'
  },
  {
    title: '待处理售后',
    name: 'afterSaleCount',
    count: '-'
  }
]

const loadData = async () => {
  let { data, err } = await api.get('/medinstrument/third/message/getMessage')
  if (err) {
    ElMessage.error(err)
  } else {
    countData?.forEach((e) => {
      e.count = data?.[e.name]
    })
    overvieData.list = data.messageList
  }
}

const showMessage = () => {
  ElMessageBox.confirm('您需要具备相关资质才能发布商品，是否立即申请资质？', '提示', {
    cancelButtonText: '取消',
    confirmButtonText: '去申请'
  })
    .then(() => {
      router.push({ path: '/userInfo' })
    })
    .catch(() => {})
}

const onDetailClick = (type) => {
  if (type === 'AUDIT_PASS') {
    router.push('/goods')
  } else if (type === 'QUALIFICATION_SOON_EXPIRE') {
    router.push('/userInfo')
  }
}

onMounted(() => {
  if (history.state.params === 'YES') {
    showMessage()
  }
  loadData()
})
</script>
<style scoped lang="scss">
.overview {
  padding: 0 15px 0 10px;
  box-sizing: border-box;

  .static {
    display: flex;
    height: 110px;
    margin: 10px 0;

    li {
      flex: 1;
      height: 110px;
      background-color: #fff;
      border-radius: 4px;
      padding: 18px;
      margin-right: 10px;
      box-sizing: border-box;

      .static-title {
        height: 16px;
        line-height: 16px;
        font-size: 16px;
        color: var(--color-text);
        font-weight: 500;
      }

      .counts {
        display: flex;
        align-items: center;
        justify-content: space-between;
        margin-top: 30px;

        .count {
          display: inline-block;
          font-size: 28px;
          color: #0486fe;
          font-weight: 500;
          line-height: 28px;
        }

        img {
          width: 30px;
          height: 30px;
        }
      }
    }

    li:last-child {
      margin-right: 0;
    }
  }

  .message {
    padding: 20px 0 30px 20px;
    margin-top: 10px;
    background-color: #fff;
    .title {
      color: var(--color-primary-text);
      font-size: 16px;
      line-height: 16px;
      font-weight: 500;
    }
    .message_info {
      margin-top: 20px;

      span {
        display: inline-block;
      }

      .tag {
        width: 65px;
        height: 24px;
        line-height: 16px;
        padding: 4px;
        margin-right: 10px;
        border-radius: 4px;
        box-sizing: border-box;

        &.account {
          background-color: #feeef0;
          color: #e88563;
        }

        &.cate {
          background-color: #eeeefe;
          color: #6381e8;
        }

        &.goods {
          background-color: #eefbfe;
          color: #498cef;
        }

        &.pay {
          background-color: #fff7e9;
          color: #e49843;
        }
      }

      .info {
        color: var(--color-text);
        height: 16px;
        line-height: 16px;
      }

      .oper {
        margin-left: 10px;

        a {
          color: #0486fe;
          font-weight: 500;
        }
      }
    }
  }
}
</style>
